package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.adapter.SearchAdapter;
import com.example.administrator.liveapp.bean.SearchPageBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.view.FullyLinearLayoutManager;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局搜索列表activity
 */

public class SearchPage extends AppCompatActivity implements View.OnClickListener{

//    private EditText ed_search_content;
//    private Button search_button;
    private RecyclerView search_recycler;
    private String seaContent;//搜索类型(文章、直播、活动)
    private SearchPageBean bean;
    private TextView search_content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_page);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        seaContent = intent.getStringExtra("seaContent");
        search_content  = (TextView)findViewById(R.id.search_content);
//        ed_search_content = (EditText) findViewById(R.id.ed_search_content);
//        search_button = (Button) findViewById(R.id.search_button);
        search_recycler = (RecyclerView) findViewById(R.id.search_recycler);
//        search_button.setOnClickListener(this);
        String url = AppUrl.URL + AppUrl.searchUrl;
        Map<String,String> map = new HashMap<>();
        map.put("seaContent",seaContent);
        XUtils.Get(url,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Gson gson = new Gson();
                bean = gson.fromJson(result,SearchPageBean.class);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                if(bean.getSearch()==null){
                    search_content.setVisibility(View.VISIBLE);
                    search_recycler.setVisibility(View.GONE);
                }else{
                    search_content.setVisibility(View.GONE);
                    search_recycler.setVisibility(View.VISIBLE);
                    recyclerView();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            case R.id.search_button:

//                break;
        }
    }

    private void recyclerView() {
        FullyLinearLayoutManager linear = new FullyLinearLayoutManager(this);
        search_recycler.setLayoutManager(linear);
        search_recycler.setHasFixedSize(true);
        SearchAdapter adapter = new SearchAdapter(bean);
        search_recycler.setAdapter(adapter);
        adapter.setOnItemClickListener(new SearchAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(bean.getSearch().get(position).getCid().equals("1")){
                    Intent intent = new Intent(SearchPage.this,LiveDetailsPage.class);
                    intent.putExtra("videoId",bean.getSearch().get(position).getSearchid());
                    startActivity(intent);
                    overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                }else if(bean.getSearch().get(position).getCid().equals("2")){
                    Intent intent = new Intent(SearchPage.this,BookingDetailsPage.class);
                    intent.putExtra("activityId",bean.getSearch().get(position).getSearchid());
                    startActivity(intent);
                    overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                }else if(bean.getSearch().get(position).getCid().equals("3")){
                    Intent intent = new Intent(SearchPage.this,ArticleDetailsPage.class);
                    intent.putExtra("articleId",bean.getSearch().get(position).getSearchid());
                    startActivity(intent);
                    overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                }
            }
        });
    }
}
